﻿using Dqs_DAL;
using Microsoft.EntityFrameworkCore;
using Model;
using Sunny.UI;
using System.Diagnostics.Metrics;
using System.Windows.Forms;

namespace Dqs_UI
{
    public partial class frmQuestion_Teacher : UIForm
    {
        MyDbContext context = new();
        public frmQuestion_Teacher()
        {
            InitializeComponent();
        }

        private void frmQuestion_Student_Load(object sender, EventArgs e)
        {
            loadData();
        }

        void loadData()
        {
            dgvQuestion.DataSource = null;
            dgvQuestion.AutoGenerateColumns = false;
            dgvQuestion.DataSource = context.Question.Include(t => t.StudentInfo).OrderByDescending(t => t.Id).Select(t => new { t.Id, t.Q, t.Status, t.StudentInfo.FullName }).ToList();
            dgvQuestion.Refresh();
        }
        private void dgvQuestion_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            DataGridViewRow row = dgvQuestion.Rows[e.RowIndex];
            // 获取行数据
            var rowData = (dynamic)row.DataBoundItem;
            if (rowData.Status != "待提问" && rowData.Status != "待确认")
            {
                frmQuestionDetail_Teacher s = new(rowData.Id);
                s.ShowDialog(this);
                loadData();
            }
        }

        private void dgvQuestion_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            var senderGrid = (DataGridView)sender;
            if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn && e.RowIndex >= 0)
            {
                var row = senderGrid.Rows[e.RowIndex];
                var question = (dynamic)row.DataBoundItem;
                if (question.Status == "待确认")
                {
                    int id = (int)question.Id;
                    var q = context.Question.Single(t => t.Id == id);
                    q.Status = "待提问";
                    context.Question.Update(q);
                    context.SaveChanges();
                    loadData();
                }
                else if (question.Status == "已回复" || question.Status == "待回复")
                {
                    frmQuestionDetail_Teacher s = new(question.Id);
                    s.ShowDialog(this);
                    loadData();
                }
            }
        }

        private void dgvQuestion_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
        {
            foreach (DataGridViewRow row in dgvQuestion.Rows)
            {
                var t = row.DataBoundItem as dynamic;

                if (t != null && t.Status == "待确认")
                {
                    row.Cells["buttonColumn"].Value = "确认";
                }
                else if (t != null && t.Status == "待提问")
                {
                    row.Cells["buttonColumn"].Value = "";
                }
                else
                {
                    row.Cells["buttonColumn"].Value = "回复";
                }
            }
        }

        private void backgroundWorker1_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(loadData));
            }
            else
            {
                loadData();
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            if (!backgroundWorker1.IsBusy)
                backgroundWorker1.RunWorkerAsync();
        }
    }
}
